<HTML><HEAD><TITLE>read(+Stream, -Term)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Term I/O</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>read(+Stream, -Term)</H1>
Succeeds if the next term from the input stream Stream is successfully read
and unified with Term.


<DL>
<DT><EM>Stream</EM></DT>
<DD>Integer (stream number) or Atom (reserved or user-defined                symbolic stream name).
</DD>
<DT><EM>Term</EM></DT>
<DD>Prolog term or variable.
</DD>
</DL>
<H2>Description</H2>
   Used to read the next term from the input stream Stream and unify it
   with Term.  If there is more than one Prolog term in the file, the term
   must be in Prolog term format i.e.  terminated by fullstop (a period and
   a blank space character), neither of which are retained by Prolog.

<P>
   Otherwise, end of file acts like fullstop.  If only end of file is read,
   the event 190 is raised and the default handler unifies Term with the
   atom end_of_file.

<P>
   The default action for syntax errors is to print a warning and fail.

<P>

<H3>Modes and Determinism</H3><UL>
<LI>read(+, -) is semidet
</UL>
<H3>Modules</H3>
This predicate is sensitive to its module context (tool predicate, see @/2).
<H3>Fail Conditions</H3>
Fails if a syntax error was detected and no term could be read
<H3>Exceptions</H3>
<DL>
<DT><EM>(4) instantiation fault </EM>
<DD>Stream is not instantiated.
<DT><EM>(5) type error </EM>
<DD>Stream is not an atom or an integer.
<DT><EM>(190) end of file reached </EM>
<DD>End of file was encountered before reading any character.
<DT><EM>(192) illegal stream mode </EM>
<DD>Stream is not an input stream.
<DT><EM>(193) illegal stream specification </EM>
<DD>Stream is an illegal stream specification.
<DT><EM>(198) reading past the file end </EM>
<DD>Trying to read even after the error 190 was raised.
</DL>
<H2>Examples</H2>
<PRE>
Success:
      [eclipse]: read(0,Term).
      &gt; atom.
      Term = atom
      yes.

      [eclipse]: open(file1,write,s),write(s, 'f(1,2,3).\ng(1,2'),
      &gt; write(s, ',3). h(1,2,3).\ni.\nj(1, 2\n,3).').
      yes.
      [eclipse]: system('cat file1').
      f(1,2,3).
      g(1,2,3). h(1,2,3).
      i.
      j(1, 2
      ,3).
      yes.
      [eclipse]: open(file1,read,s), read(s,A), read(s,B),
      &gt; read(s,C), read(s,D), read(s,E), read(s,F).
      A = f(1, 2, 3)
      B = g(1, 2, 3)
      C = h(1, 2, 3)
      D = i
      E = j(1, 2, 3)
      F = end_of_file
      yes.
Fail:
      [eclipse]: read(0,a).
      &gt; b.
      no.

      [eclipse]: read(0,X).
      &gt; f(1,2)m.
              ^ (here?)
      syntax error: postfix/infix operator expected
      no (more) solution.
Error:
      read(a(b,c),S).               (Error 4).
      read("string", a(b,c)).       (Error 5).
      read(9, X=2).                 (Error 192). % stream not open
      read(atom, X=2).              (Error 193).



</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/ioterm/read-1.html">read / 1</A>, <A HREF="../../kernel/ioterm/readvar-3.html">readvar / 3</A>, <A HREF="../../kernel/iochar/read_token-2.html">read_token / 2</A>, <A HREF="../../kernel/iochar/read_token-3.html">read_token / 3</A>
</BODY></HTML>
